package com.ruoyi.library.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.CalendarUtil;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.library.domain.LibBook;
import com.ruoyi.library.domain.dto.page.LibBookPageDTO;
import com.ruoyi.library.domain.dto.update.LibBookUpdateStatusDTO;
import com.ruoyi.library.domain.vo.LibBookVO;
import com.ruoyi.library.mapper.LibBookMapper;
import com.ruoyi.library.service.ILibBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 图书馆-图书Service业务层处理
 *
 * @author xudengyu
 * @date 2025-03-12
 */
@Service
@Slf4j
public class LibBookServiceImpl implements ILibBookService {

    // 无库存
    public static final Long STOCK_EMPTY = 0L;
    // 库存不足
    public static final Long STOCK_INSUFFICIENT = 4L;
    // 库存充足
    public static final Long STOCK_SUFFICIENT = 5L;

    @Resource
    private LibBookMapper libBookMapper;

    /**
     * 改变当前图书的状态
     *
     * @param libBookUpdateStatusDTO update
     * @return int
     */
    @Override
    public int changeBookStatus(LibBookUpdateStatusDTO libBookUpdateStatusDTO) {
        LibBook libBook = BeanUtil.copyProperties(libBookUpdateStatusDTO, LibBook.class);
        return updateLibBook(libBook);
    }

    /**
     * 判断是否在日期范围内
     *
     * @param before      before
     * @param after       after
     * @param publishTime publishTime
     * @return boolean
     */
    private boolean judgeRangeMonth(String before, String after, String publishTime) {
        // 定义格式化器（兼容不同格式）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("[yyyy-MM][yyyy-MM-dd]");
        YearMonth beforeYearMonth = YearMonth.parse(before, formatter);
        YearMonth afterYearMonth = YearMonth.parse(after, formatter);
        YearMonth publishTimeYearMonth = YearMonth.parse(publishTime, formatter);
        log.info("## before: {}", beforeYearMonth);
        log.info("## after: {}", afterYearMonth);
        log.info("## publishTime: {}", publishTimeYearMonth);
        return publishTimeYearMonth.equals(beforeYearMonth) ||
                publishTimeYearMonth.equals(afterYearMonth) ||
                (publishTimeYearMonth.isAfter(beforeYearMonth) &&
                        publishTimeYearMonth.isBefore(afterYearMonth)
                );
    }

    /**
     * 判断库存规则
     *
     * @param bookStockRule bookStockRule
     * @param libBookStock  libBookStock
     * @return boolean
     */
    private boolean judgeStockRule(Long bookStockRule, Long libBookStock) {
        // 0-无库存；4-库存不足；5-库存充足
        if (STOCK_EMPTY.equals(bookStockRule)) return STOCK_EMPTY.equals(libBookStock);
        else if (STOCK_INSUFFICIENT.equals(bookStockRule)) return STOCK_INSUFFICIENT >= libBookStock;
        else if (STOCK_SUFFICIENT.equals(bookStockRule)) return STOCK_SUFFICIENT <= libBookStock;
        return false;
    }

    /**
     * 条件分页查询
     *
     * @param libBookPageDTO page
     * @return list
     */
    @Override
    public List<LibBookVO> pageLibBookList(LibBookPageDTO libBookPageDTO) {
        LibBook libBook = BeanUtil.copyProperties(libBookPageDTO, LibBook.class);
        ArrayList<LibBookVO> res = new ArrayList<>();
        selectLibBookList(libBook).forEach(i -> {
            // 出版日期范围
            if (libBookPageDTO.getRangeMonth() != null) {
                boolean f = judgeRangeMonth(
                        libBookPageDTO.getRangeMonth()[0],
                        libBookPageDTO.getRangeMonth()[1],
                        i.getLibBookPublishTime()
                );
                if (!f) return;
            }
            // 库存判断
            if (libBookPageDTO.getLibBookStockRule() != null) {
                boolean f = judgeStockRule(libBookPageDTO.getLibBookStockRule(), i.getLibBookStock());
                if (!f) return;
            }
            LibBookVO libBookVO = BeanUtil.copyProperties(i, LibBookVO.class);
            res.add(libBookVO);
        });
        return res;
    }

    /**
     * 查询图书馆-图书
     *
     * @param libBookId 图书馆-图书主键
     * @return 图书馆-图书
     */
    @Override
    public LibBook selectLibBookByLibBookId(Long libBookId) {
        return libBookMapper.selectLibBookByLibBookId(libBookId);
    }

    /**
     * 查询图书馆-图书列表
     *
     * @param libBook 图书馆-图书
     * @return 图书馆-图书
     */
    @Override
    public List<LibBook> selectLibBookList(LibBook libBook) {
        return libBookMapper.selectLibBookList(libBook);
    }

    /**
     * 新增图书馆-图书
     *
     * @param libBook 图书馆-图书
     * @return 结果
     */
    @Override
    public int insertLibBook(LibBook libBook) {
        return libBookMapper.insertLibBook(libBook);
    }

    /**
     * 修改图书馆-图书
     *
     * @param libBook 图书馆-图书
     * @return 结果
     */
    @Override
    public int updateLibBook(LibBook libBook) {
        return libBookMapper.updateLibBook(libBook);
    }

    /**
     * 批量删除图书馆-图书
     *
     * @param libBookIds 需要删除的图书馆-图书主键
     * @return 结果
     */
    @Override
    public int deleteLibBookByLibBookIds(Long[] libBookIds) {
        return libBookMapper.deleteLibBookByLibBookIds(libBookIds);
    }

    /**
     * 删除图书馆-图书信息
     *
     * @param libBookId 图书馆-图书主键
     * @return 结果
     */
    @Override
    public int deleteLibBookByLibBookId(Long libBookId) {
        return libBookMapper.deleteLibBookByLibBookId(libBookId);
    }
}
